﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ESF.MUP.BasePlatform.Domain.Model;
using ESF.MUP.BasePlatform.Domain.Repositories;
using ESF.MUP.BasePlatform.Infrastructure.Dal;
using ESF.MUP.BasePlatform.ApplicationServices.Interfaces;

namespace ESF.MUP.BasePlatform.Services
{
    public class ApplicationManagementService : IApplicationManagementService
    {
        private IApplicationRepository _applicationRepository;

        #region "Constructor"

        /// <summary>
        /// Create a new instance of Application management service
        /// </summary>
        /// <param name="applicationRepository"></param>
        public ApplicationManagementService(IApplicationRepository applicationRepository)
        {
            _applicationRepository = applicationRepository;
        }

        #endregion

        public void AddNew(Application application)
        {
            _applicationRepository.Add(application);
            _applicationRepository.UnitOfWork.Commit();

        }

        public List<Application> GetAll()
        {
            return _applicationRepository.GetAll().ToList();
        }


        public Application GetByName(string name)
        {
            return _applicationRepository.GetFiltered(a => String.Compare(a.Name, name, StringComparison.InvariantCultureIgnoreCase) == 0)
                    .SingleOrDefault();
        }

        public Application GetById(Int32 id)
        {
            return _applicationRepository.GetFiltered(a => a.Id == id)
                    .SingleOrDefault();
        }

        public void Add(Application application)
        {
            _applicationRepository.Add(application);
            _applicationRepository.UnitOfWork.Commit();

        }

        public void Delete(Application application)
        {
            _applicationRepository.Remove(application);
            _applicationRepository.UnitOfWork.Commit();
        }

        public void Update(Application application)
        {
            _applicationRepository.TrackItem(application);
            _applicationRepository.UnitOfWork.Commit();
        }

    }
}
